<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item >
        <el-input v-model="dataForm.username" placeholder="会员码/用户名" clearable></el-input>
      </el-form-item >
      <el-form-item label="" >
        <el-select v-model="dataForm.vip" placeholder="会员组" clearable>
          <el-option
            v-for="item in options3"
            :key="item.value3"
            :label="item.label3"
            :value="item.value3">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="注册时间:" v-show="iszan">
        <el-date-picker
          v-model="value2"
          :format="'yyyy-MM-dd HH:mm:ss'"
          :value-format="'yyyy-MM-dd HH:mm:ss'"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="" v-show="iszan">
        <el-select v-model="dataForm.is_lock" placeholder="状态" clearable>
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="" v-show="iszan">
        <el-select v-model="dataForm.username"  placeholder="用户名" clearable>
          <el-option
            v-for="item in options4"
            :key="item.value4"
            :label="item.label4"
            :value="item.value4">
          </el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item v-show="iszan">
        <el-input v-model="dataForm.email" placeholder="邮箱" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('member:v9member:save')" type="primary" @click="addOrUpdateHandle()">添加会员</el-button>
        <el-link v-show="!iszan" @click="zankai()"  :underline="false" class="zankai">展开 <i class="el-icon-arrow-down"></i></el-link>
        <el-link v-show="iszan" @click="zankai()"  :underline="false" class="zankai">收起 <i class="el-icon-arrow-up"></i></el-link>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        min-width=""
        label="用户名">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        min-width=""
        label="会员识别码">
      </el-table-column>
      <el-table-column
        prop="inviteCode"
        header-align="center"
        align="center"
        min-width=""
        label="我的上级">
      </el-table-column>
      <!--<el-table-column
        prop="isLock"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isLock ===0" size="small" >正常</el-tag>
          <el-tag v-if="scope.row.isLock ===1" size="small" >锁定</el-tag>
        </template>
      </el-table-column>-->
      <el-table-column
        prop="nickname"
        header-align="center"
        align="center"
        min-width=""
        label="昵称">
      </el-table-column>
      <el-table-column
        prop="email"
        header-align="center"
        align="center"
        min-width="170"
        label="邮箱">
      </el-table-column>
      <el-table-column
        prop="vip"
        header-align="center"
        align="center"
        min-width=""
        label="会员组">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.vip ===0" size="small" >普通会员</el-tag>
          <el-tag v-if="scope.row.vip ===1" size="small" >青铜会员</el-tag>
          <el-tag v-if="scope.row.vip ===2" size="small" >白银会员</el-tag>
          <el-tag v-if="scope.row.vip ===3" size="small" >黄金会员</el-tag>
          <el-tag v-if="scope.row.vip ===4" size="small" >铂金会员</el-tag>
          <el-tag v-if="scope.row.vip ===5" size="small" >钻石会员</el-tag>
          <el-tag v-if="scope.row.vip ===6" size="small" >终生会员</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="regip"
        header-align="center"
        align="center"
        min-width="120"
        label="注册IP">
      </el-table-column>
      <el-table-column
        prop="lastdateStr"
        header-align="center"
        align="center"
        min-width="130"
        label="最后登录">
      </el-table-column>
      <el-table-column
        prop="point"
        header-align="center"
        align="center"
        label="积分点数">
      </el-table-column>
      <el-table-column
        prop="amount"
        header-align="center"
        align="center"
        :formatter="formatMoney"
        label="余额">
      </el-table-column>
      <el-table-column
        prop="kuaibi"
        header-align="center"
        align="center"
        label="筷币">
      </el-table-column>
      <el-table-column
        prop="defaultKefu"
        header-align="center"
        align="center"
        label="客服">
      </el-table-column>
     <!-- <el-table-column
        prop="kefuId"
        header-align="center"
        align="center"
        label="客服id">
      </el-table-column>-->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.userid)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <add-or-update v-if="memberUpdateVisible" ref="memberUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './v9member-add-or-update'
  import MemberUpdate from './memberUpdate'
  export default {
    data () {
      return {
        value: '',
        visible: {},
        options: [{
          value: 0,
          label: '正常'
        }, {
          value: 1,
          label: '锁定'
        }],
        value3: '',
        visible3: {},
        options3: [{
          value3: 0,
          label3: '普通会员'
        }, {
          value3: 1,
          label3: '青铜会员'
        }, {
          value3: 2,
          label3: '白银会员'
        }, {
          value3: 3,
          label3: '黄金会员'
        }, {
          value3: 4,
          label3: '铂金会员'
        }, {
          value3: 5,
          label3: '钻石会员'
        }, {
          value3: 6,
          label3: '终生会员'
        }],
        dataForm: {
        },
        keyword: '',
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        memberUpdateVisible: false,
        iszan: false,
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick (picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick (picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        value1: [new Date(2000, 10, 10, 10, 10), new Date(2000, 10, 11, 10, 10)],
        value2: ''
      }
    },
    components: {
      AddOrUpdate,
      MemberUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      zankai: function () {
        this.iszan = !this.iszan;
      },
      formatMoney: function (row, column) {
        // eslint-disable-next-line no-return-assign
        return row.amountStr = '¥' + parseFloat(row.amount).toFixed(2) ;
      },
      formatRole: function (row, column) {
        // eslint-disable-next-line no-return-assign
        return row.regdateStr = this.timestampToTime(row.regdate)
      },
      timestampToTime (timestamp) {
        var date = new Date(timestamp)//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        var D = date.getDate() + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        return Y + M + D + h + m + s;
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let start, end;
        if (this.value2) {
          start = this.value2[0];
          end = this.value2[1];
        }
        this.$http({
          url: this.$http.adornUrl('/member/v9member/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'is_lock': this.dataForm.is_lock,
            'vip': this.dataForm.vip,
            'username': this.dataForm.username,
            'userid': this.dataForm.userid,
            'email': this.dataForm.email,
            'phpssouid': this.dataForm.phpssouid,
            'start': start,
            'end': end
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      memberUpdateHandle (id) {
        this.memberUpdateHandle = true
        this.$nextTick(() => {
          this.$refs.memberUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.userid
        })
        this.$confirm(`确定进行'删除'操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/member/v9member/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
